package top.kaoshanji.p1ch02e;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/1/4 18:47
 */
public class MethodHandlesLookupExample05 {

    private static final Logger logger = LogManager.getLogger(MethodHandlesLookupExample05.class);

    public static void main(String[] args) throws Throwable {

        new MethodHandlesLookupExample05().accessControl();

    }

    public void accessControl() throws Throwable {
        logger.info("...代码清单 p1ch02e-02：方法句柄查找时的访问控制权限的使用示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle mh = lookup.findSpecial(MethodHandlesLookupExample05.class, "privateMethod",
                MethodType.methodType(void.class), MethodHandlesLookupExample05.class);
        mh = mh.bindTo(this);

        mh.invoke();
    }


    private void privateMethod() {
        System.out.println("PRIVATE");
    }

}
